package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.entity.Child;
import com.youtellv1.service.ChildService;
import com.youtellv1.service.ChildSignService;
import com.youtellv1.utils.SignDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/manager/childSign")
public class ChildSignController {

    @Autowired
    private ChildSignService childSignService;

    @Autowired
    private ChildService childService;

    @GetMapping("/info")
    @ResponseBody
    public ResultData<Map<String, Object>> getChildSignInfo(@RequestParam("cid") Integer cid) {
        Map<String, Object> result = new HashMap<>();

        Child child = childService.findNameByCid(cid);
        result.put("cname", child.getCname());

        boolean signedToday = childSignService.isTodaySigned(cid);
        result.put("flag", signedToday);

        List<Date> allSignDates = childSignService.findAllSignDates(cid);
        result.put("timelist", allSignDates);

        List<Date> monthSignDates = childSignService.findThisMonthSignDates(cid);
        result.put("mlist", monthSignDates);

        int continuousDays = SignDateUtils.calculateContinuousWorkdaySignIns(allSignDates);
        result.put("continuous", continuousDays);

        return new ResultData<>(result);
    }

    @GetMapping("/checksign_c")
    @ResponseBody
    public String checkChildSign(@RequestParam("id") Integer cid,
                                 @RequestParam("cont") String cont) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date signDate = format.parse(cont);

            boolean exists = childSignService.hasSignedOnDate(cid, signDate);
            return exists ? "1" : "";
        } catch (Exception e) {
            return "error";
        }
    }

    @GetMapping("/rep_sign_c")
    public ResultData<Void> repairSign(@RequestParam("id") Integer cid,
                                       @RequestParam("cont") String cont) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date signDate = format.parse(cont);
            childSignService.insertSignRecord(cid, signDate);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "补签失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/sign_in")
    public ResultData<Void> signInToday(@RequestParam("cid") Integer cid) {
        try {
            if (childSignService.hasSignedToday(cid)) {
                return new ResultData<>(400, "今日已签到，无需重复签到", null);
            }
            childSignService.insertTodaySign(cid);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "签到失败：" + e.getMessage(), null);
        }
    }

}
